package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/business"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type QuestionApi struct {
}

var questionService = service.ServiceGroupApp.BusinessServiceGroup.QuestionService


// CreateQuestion 创建问题
// @Tags Question
// @Summary 创建问题
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Question true "创建问题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /question/createQuestion [post]
func (questionApi *QuestionApi) CreateQuestion(c *gin.Context) {
	var question business.Question
	err := c.ShouldBindJSON(&question)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := questionService.CreateQuestion(&question); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteQuestion 删除问题
// @Tags Question
// @Summary 删除问题
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Question true "删除问题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /question/deleteQuestion [delete]
func (questionApi *QuestionApi) DeleteQuestion(c *gin.Context) {
	var question business.Question
	err := c.ShouldBindJSON(&question)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := questionService.DeleteQuestion(question); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteQuestionByIds 批量删除问题
// @Tags Question
// @Summary 批量删除问题
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除问题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /question/deleteQuestionByIds [delete]
func (questionApi *QuestionApi) DeleteQuestionByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := questionService.DeleteQuestionByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateQuestion 更新问题
// @Tags Question
// @Summary 更新问题
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.Question true "更新问题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /question/updateQuestion [put]
func (questionApi *QuestionApi) UpdateQuestion(c *gin.Context) {
	var question business.Question
	err := c.ShouldBindJSON(&question)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := questionService.UpdateQuestion(question); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindQuestion 用id查询问题
// @Tags Question
// @Summary 用id查询问题
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query business.Question true "用id查询问题"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /question/findQuestion [get]
func (questionApi *QuestionApi) FindQuestion(c *gin.Context) {
	var question business.Question
	err := c.ShouldBindQuery(&question)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if requestion, err := questionService.GetQuestion(question.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"requestion": requestion}, c)
	}
}

// GetQuestionList 分页获取问题列表
// @Tags Question
// @Summary 分页获取问题列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query businessReq.QuestionSearch true "分页获取问题列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /question/getQuestionList [get]
func (questionApi *QuestionApi) GetQuestionList(c *gin.Context) {
	var pageInfo businessReq.QuestionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := questionService.GetQuestionInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
